﻿using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ProfileApplication;

namespace CrossEntropyProject.GUI
{
    public partial class chartsForm : Form
    {
        private MainForm mForm;
        private bool regularChartLoad;

        #region Constructor
        public chartsForm(MainForm mainForm, bool LoadRegularDataFlag)
        {
            mForm = mainForm;
            regularChartLoad = LoadRegularDataFlag;
            InitializeComponent();
            LoadChartsIntoForm();
        } 
        #endregion

        #region LoadChartsIntoForm()
        public void LoadChartsIntoForm()
        {
           int numProfiles = Profiles.profilesList.Count();
           List<Profile> pfList = new List<Profile>();

            for (int i = 0; i < numProfiles; i++)
            {
                pfList.Add(new Profile(Profiles.profilesList[i], regularChartLoad));
                pfList[i].title1.Text = "Customer Profile " + (i + 1).ToString();
            }

            //calculate the number of rows and cols needed in the tableLayout

            int numCols = ClientSize.Width / pfList[0].chart1.Size.Width; //number of charts can be fitted in row
            int numRows = ClientSize.Height / pfList[0].chart1.Size.Height;

            int placedChartsCount = 0;
            int row = 0;
            int col = 0;

            for (int j = 0; j < pfList.Count; j++, col++)
            {
                if (placedChartsCount == numCols)
                {
                    row++;
                    col = 0;
                    placedChartsCount = 0;
                }
                if (placedChartsCount < numCols)
                {
                    pfList[j].chart1.Location = new Point(12 + col * (pfList[j].chart1.Size.Width + 12), 12 + row * (pfList[j].chart1.Size.Height + 12));
                    Controls.Add(pfList[j].chart1);
                    placedChartsCount++;
                }
            }

            ClientSize = new Size((12 + numCols * (pfList[0].chart1.Size.Height + 12)) + 12, 12 + (row + 1) * (pfList[0].chart1.Size.Width + 12));

        } 
        #endregion

        #region chartsForm_FormClosing
        internal void chartsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MainForm.chartShowingFlag = false;
            mForm.CloseTogether();
        } 
        #endregion

    }
}
